<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>写笔记-桃子同学的个人空间</title>
    <link rel="icon" href="/public/images/index/about/favicon.ico" >
    <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="/public/styles/base.css"/>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/3.2.1/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/public/styles/admin/login.css"/>
    <link rel="stylesheet" href="/public/styles/admin/left-menu.css"/>
    <style>
        html,body{height: 100%;}
        .outer{height: 100%;opacity: 0;}
        .content{margin-left: 70px;background-color:#f3f3f4;padding-top: 20px;min-height: 100%;}
        .content input{border: 1px solid #ccc;box-sizing: border-box;display: block;height: 40px;margin: 0 auto 15px;padding-left: 15px;width: 80%;border-radius: 3px;}
        .abstract{border: 1px solid #ccc;box-sizing: border-box;display: block;height: 70px;margin: 0 auto 15px;padding:5px 15px;width: 80%;border-radius: 3px;}
        #ueditor{width: 95%;margin: 0 auto;}
        .publish-blog,.local-storage{border: 1px solid #ccc;border-radius: 3px;display: inline-block;height: 40px;line-height: 40px;margin: 18px 9px 10px 0;text-align: center;width: 140px;}
        .publish-blog i{margin-right: 2px;display: none;}
        .publish-blog span{vertical-align: middle;display: inline-block}
        .publish-blog.active {cursor: default;}
        .publish-blog.active i{display: inline-block}
        .publish-blog.active span{display: none}
    </style>
</head>
<body>
<div class="outer">
    <if condition="$loginStatus eq 0 ">
        <input type="hidden" value="{$URL_PREFIX}" id="urlPrefix"/>
        <include file="login"/>
    </if>
    <include file="left_menu"/>
    <div class="content">
        <input name="title" type="text" placeholder="标题" value="{$data.title}"/>
        <input name="tag" type="text" placeholder="标签(多个以空格分开)" value="{$data.tag}"/>
        <textarea name="abstract" class="abstract" placeholder="摘要信息">{$data.abstract}</textarea>
        <script id="ueditor">{$data.content}</script>
        <div class="text-center">
            <a href="javascript:;" class="local-storage">保存</a><a class="publish-blog" href="javascript:;"><i class="icon-spinner icon-large icon-spin"></i><span>发布</span></a>
        </div>
    </div>
</div>
<script src="//cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script src="/public/scripts/ueditor/ueditor.config.js"></script>
<script src="/public/scripts/ueditor/ueditor.all.min.js"></script>
<script src="/public/scripts/base.js"></script>
<script src="/public/scripts/admin/login.js"></script>
<script src="/public/scripts/admin/left-menu.js"></script>
<script>
    //富文本编辑器
    var ue;
    function ueditor(){
        ue = UE.getEditor('ueditor',{
            initialFrameWidth : '100%',
            initialFrameHeight : 300,
            maximumWords : 30000,
            elementPathEnabled : false,
            onready:function(){
                this.on('showmessage', function(type, m){
                    if (m['content'] == '本地保存成功') {
                        return true;
                    }
                });

            }
        });
    }
    //保存到本地
    function dataLocalStorage(){
        if(localStorage.title) $("input[name='title']").val(localStorage.title);
        if(localStorage.tag) $("input[name='tag']").val(localStorage.title);
        if(localStorage.abstract) $(".abstract").val(localStorage.abstract);
        ue.addListener("ready", function () {
            if(localStorage.content) ue.setContent(localStorage.content);
        });
        $(".local-storage").click(function(){
            localStorage.title = $("input[name='title']").val();
            localStorage.tag = $("input[name='tag']").val();
            localStorage.abstract = $(".abstract").val();
            localStorage.content = ue.getContent();
            alert("保存成功");
        });
    }
    //发布
    function publish(){
        $(".publish-blog").click(function(){
            //清空本地存储
            localStorage.title = "";
            localStorage.tag = "";
            localStorage.abstract = "";
            localStorage.content = "";
            var current = $(this);
            if(current.hasClass("active")) return false;
            current.addClass("active");
            var title = $("input[name='title']").val();
            var tag = $("input[name='tag']").val();
            var abstract = $(".abstract").val();
            var content = ue.getContent();
            var id = -1;
            if("{$data.id}" != "") id = "{$data.id}";
            $.ajax({
                url:"/{$URL_PREFIX}admin/publish",
                type:"post",
                data:({id:id,title:title,tag:tag,abstract:abstract,content:content}),
                success:function(data){
                    setTimeout(function(){
                        if(data.status == 1) current.removeClass("active").find("span").text("发布成功");
                        else current.removeClass("active").find("span").text("发布失败");
                    },500);
                },
                error:function(){
                    current.removeClass("active").find("span").text("网络错误");
                }
            });
        });
        $("input,textarea").click(function(){
            $(".publish-blog span").text("发布");
        });
        ue.addListener("focus", function () {
            $(".publish-blog span").text("发布");
        });
    }
    //统一执行入口
    $(function(){
        ueditor();
        dataLocalStorage();
        publish();
    })
</script>
</body>
</html>